﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using PBOffice.BLL;
using PBOffice.Model;

namespace Web
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        BranchManager braMan = new BranchManager();
        protected void Page_Load(object sender, EventArgs e)
        {
            

        }
        /// <summary>
        /// 添加功能
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void BtnAdd_Click(object sender, EventArgs e)
        {
            Branch newBra = new Branch();
            if(this.txtBranchName.Text==""||this.txtBranchShortName.Text=="")
            {
                Page.ClientScript.RegisterClientScriptBlock(typeof(string), "error", "alert('机构名称与机构简称不能为空！');", true);
                return;
            }
            newBra.BranchName = this.txtBranchName.Text;
            newBra.BranchShortName = this.txtBranchShortName.Text;
            newBra.PrincipalUser = this.txtPrincipalUser.Text;
            newBra.ConnectTelNo = this.txtConnectTelNo.Text;
            newBra.ConnectMobileTelNo = this.txtConnectMobileTelNo.Text;
            newBra.Faxes = this.txtFaxes.Text;
            

            bool result = braMan.AddBranch(newBra);
            if (result)
            {
                Page.ClientScript.RegisterClientScriptBlock(typeof(string), "OK", "alert('添加成功！');", true);
                this.BranchGDV.DataSourceID = null;
                this.BranchGDV.DataSource = braMan.GetAllBranch();
                this.BranchGDV.DataBind();
                this.txtBranchName.Text = "";
                this.txtBranchShortName.Text = "";
                this.txtConnectTelNo.Text = "";
                this.txtConnectMobileTelNo.Text = "";
                this.txtFaxes.Text = "";
                this.txtPrincipalUser.Text = "";
                    
            }
            else
            {
                Page.ClientScript.RegisterClientScriptBlock(typeof(string), "error", "alert('参数错误！');", true);
            }

        }
        /// <summary>
        /// 选择编辑功能
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void BranchGDV_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {
            txtBranchName.Text = BranchGDV.Rows[e.NewSelectedIndex].Cells[0].Text;
            txtBranchShortName.Text = BranchGDV.Rows[e.NewSelectedIndex].Cells[1].Text;
            txtPrincipalUser.Text = BranchGDV.Rows[e.NewSelectedIndex].Cells[2].Text;
            txtConnectTelNo.Text = BranchGDV.Rows[e.NewSelectedIndex].Cells[3].Text;

            List<Branch> listBra = braMan.GetSingleBranch(BranchGDV.Rows[e.NewSelectedIndex].Cells[0].Text);
            txtConnectMobileTelNo.Text = listBra[0].ConnectMobileTelNo;
            txtFaxes.Text = listBra[0].Faxes;
            
        }
        /// <summary>
        /// 保存修改按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void BtnUpdate_Click(object sender, EventArgs e)
        {
            Branch braUpdate = new Branch();
            braUpdate.BranchId = (int)BranchGDV.DataKeys[BranchGDV.SelectedIndex].Value;
            braUpdate.BranchName = txtBranchName.Text.Trim();
            braUpdate.BranchShortName = txtBranchShortName.Text.Trim();
            braUpdate.ConnectMobileTelNo = txtConnectMobileTelNo.Text.Trim();
            braUpdate.PrincipalUser = txtPrincipalUser.Text.Trim();
            braUpdate.ConnectTelNo = txtConnectTelNo.Text.Trim();
            braUpdate.Faxes = txtFaxes.Text.Trim();

            bool result = braMan.UpdateBranch(braUpdate);
            if (result)
            {
                Page.ClientScript.RegisterClientScriptBlock(typeof(string), "OK", "alert('添加成功！');", true);
                this.BranchGDV.DataSourceID = null;
                this.BranchGDV.DataSource = braMan.GetAllBranch();
                this.BranchGDV.DataBind();
                this.txtBranchName.Text = "";
                this.txtBranchShortName.Text = "";
                this.txtConnectTelNo.Text = "";
                this.txtConnectMobileTelNo.Text = "";
                this.txtFaxes.Text = "";
                this.txtPrincipalUser.Text = "";
            }
            else 
            {
                Page.ClientScript.RegisterClientScriptBlock(typeof(string), "error", "alert('参数错误！');", true);
            }


        }
    }
}